package ATM1;

import java.util.Scanner;

public class ATM {
    /**
     * 方法版本的ATM
     */
    public static Scanner sc = new Scanner(System.in);
    private int userID = 0;
    private User[] users = {
            new User( "aaa" , "111" ,5000.0 ),
            new User( "bbb" , "222" , 2500.0),
            new User( "ccc" , "333" ,2000.0 ),
            new User( "ddd" , "444" , 6600.0),
    };

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID){
        this.userID = userID;
    }
    public User[] getUser(){
        return users;
    }
    public void setUser(User[] users){
        this.users = users;
    }

    /**
     * 主流程方法
     */
    public static void process(){
        ATM atm = new ATM();
        System.out.println("欢迎使用XXX银行");
        while (true) {
            login(atm);
            menu(atm);
        }
    }
    /**
     * 登录
     */
    public static void login(ATM atm){
        for(int i = 0;i < 3;i++){
            //登录判断
            boolean isLogin = false;
            System.out.println("请输入账号");
            String account = sc.next();
            System.out.println("请输入密码");
            String pwd = sc.next();

            for (int j = 0;j < atm.users.length;j++){
                if (atm.users[j].getUserName().equals(account) && atm.users[j].getUserPwd().equals(pwd)){
                    System.out.println("登录成功");
                    isLogin = true;
                    break;
                }
            }
            if(isLogin){
                break;
            }else {
                System.out.println("登录失败，还剩" + (2 - i) +"次机会");
                if(i == 2){
                    System.out.println("账户冻结");
                    System.exit(0);
                }
            }
        }

    }


    /**
     * 菜单
     */
    public static void menu(ATM atm){
        System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-退出 6-修改密码 7-切换账号");
        int input = sc.nextInt();
        if(input == 1){
            save(atm);
        }else if(input == 2){
            get(atm);
        }else if (input == 3){
            search(atm);
        }else if (input == 4){
            transfer(atm);
        }else if (input == 5){
            System.out.println("再见");
            System.exit(0);
        }else if (input == 6){
            changePwd(atm);
        }else if(input == 7 ){
            changeAcc(atm);
        }else {
            System.out.println("输入有误");
        }
    }

    /**
     * 存款
     */
    public static void save(ATM atm) {
        System.out.println("请输入存款金额：");
        double blance = sc.nextDouble();
        if (blance > 0) {
            atm.users[atm.userID].setBalance(Double.parseDouble(atm.users[atm.userID].getBalance() + blance + ""));
            System.out.println("存款成功！");
        } else {
            System.out.println("金额输入有误");
        }
        menu(atm);
    }

    /**
     * 取款
     */
    public static void get(ATM atm){
        System.out.println("请输入取款金额");
        double blance = sc.nextDouble();
        if (blance <= atm.users[atm.userID].getBalance() && blance > 0){
            atm.users[atm.userID].setBalance(atm.users[atm.userID].getBalance() - blance);
            System.out.println("取款成功！");
            menu(atm);
        }else {
            System.out.println("取款失败");
        }
        menu(atm);
    }

    /**
     * 查询余额
     */
    public static void search(ATM atm){
        System.out.println("您的余额为："+atm.users[atm.userID].getBalance() + "元");
        menu(atm);
    }

    /**
     * 转账
     */
    public static void transfer(ATM atm) {
        System.out.println("请输入对方账号：");
        String otherAccount = sc.next();
        // 不能转账给自己
        if (otherAccount.equals(atm.users[atm.userID].getUserName())) {
            System.out.println("不能转给自己");
            menu(atm);
        }
        // 判断对方账号是否存在
        boolean isExist = false;
        int otherUser = -1;
        for (int j = 0; j < atm.users.length; j++) {
            if (otherAccount.equals(atm.users[j].getUserName())) {
                isExist = true;
                otherUser = j;
                break;
            }
        }
        if (!isExist) {
            System.out.println("输入的对方账号错误");
            menu(atm);
        }
        System.out.println("请输入转账金额：");
        double blance = sc.nextDouble();
        // 判断余额是否足够
        if (blance <= atm.users[atm.userID].getBalance() && blance > 0) {
            atm.users[atm.userID].setBalance(atm.users[atm.userID].getBalance() - blance);
            atm.users[otherUser].setBalance(atm.users[otherUser].getBalance() + blance);
            System.out.println("转账成功");
            menu(atm);
        } else {

            System.out.println("转账失败");
        }
        menu(atm);
    }

    /**
     * 修改密码
     */


    public static void changePwd(ATM atm){
        System.out.println("请输入原密码");
        String Pwd1 = sc.next();
        boolean chn = false;
        if(Pwd1.equals(atm.users[atm.userID].getUserPwd())) {
            chn = true;
        }else {
            System.out.println("原密码不正确");
            return;
        }
        if(chn){
            System.out.println("请输入新密码");
            String newPwd = sc.next();
            System.out.println("请再次确定您的新密码");
            String newPwd1 =sc.next();
            if(newPwd1.equals(newPwd)){
                System.out.println("密码修改成功");
                atm.users[atm.userID].setUserPwd(newPwd1);
            }else {
                System.out.println("确认密码输入错误，无法修改");
                return;
            }
        }
        menu(atm);
    }
    public static void changeAcc(ATM atm){
        login(atm);
    }

}
